﻿using System;
using System.Threading;

namespace SuperKit {
    public class DirectoryExecutorItem {
        public const int READY = 0;
        public const int EXECUTE = 1;
        public const int COMPLETE = 2;

        public string Dir { get; }
        public DirectoryExecutor Parent { get; }
        public int Status { get; set; }
        public int Index { get; set; }
        public int LogIndex { get; set; }
        public string Cmd { get; private set; }

        public DirectoryExecutorItem(string dir, DirectoryExecutor parent) {
            Dir = dir;
            Parent = parent;
            Status = READY;
        }

        public void ExecuteShell() {
            Status = EXECUTE;
            Thread t = new Thread(RunShell);
            t.Start();
        }

        private void RunShell() {
            Cmd = string.Format("{0} -close -shell -shell_idx {2} -d \"{1}\"", Parent.Parent.GetArgText(), Dir, LogIndex);
            Parent.Parent.Flush($"Execute:{Cmd}");
            ShellKit.ExecuteCommand(Cmd);
            Status = COMPLETE;
        }

        public void Execute() {
            Status = EXECUTE;
            Thread t = new Thread(Run);
            t.Start();
        }

        private void Run() {
            ArgReader arg = new ArgReader(Parent.Parent.GetArgText(), Dir);
            arg.Owner = this;
            arg.Execute(Parent.Parent);
            Status = COMPLETE;
        }

    }
}
